package com.example.hotelsystem.controller.userrolepower;


import com.example.hotelsystem.entity.RolePower;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.RolePowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lee
 * @since 2020-10-13
 */
@RestController
@RequestMapping("/hotelsystem/RolePower")
@Api(tags = "角色权限关联控制器")
public class RolePowerController {
    @Autowired
    RolePowerService RolePowerService;

    @ApiOperation("查看某个角色拥有的权限(传入角色id参数)")
    @PostMapping("/getPower")
    public Result GetPowerByRole(@RequestParam("role_id")Long roleId){
        return RolePowerService.getRolePower(roleId);
    }


    @ApiOperation("为角色批量添加权限,(传入参数-权限的ids数组和角色roleId)")
    @PostMapping("/addPower")
    public Result AddPowerToRole(Long[] ids,Long roleId){
        List<String> errorList = new ArrayList<String>();
        for(long i: ids){
            RolePower RolePower = new RolePower();
            RolePower.setPowerId(i);
            RolePower.setRoleId(roleId);
            boolean isAdd = RolePowerService.saveOrUpdate(RolePower);
            if(isAdd){continue;}else{
                errorList.add("第"+i+"号权限添加失败");
            }
        }
        return Result.success("操作成功",errorList);
    }

    @ApiOperation("为角色批量删除权限,(传入参数-角色权限关联的ids数组)")
    @PostMapping("/removePower")
    public Result RemovePowerToRole(Long[] ids){
        List<String> errorList = new ArrayList<String>();
        for(long i: ids){
            boolean isRemove = RolePowerService.removeById(i);
            if(isRemove){continue;}else{
                errorList.add("第"+i+"号权限删除失败");
               };
        }
        return Result.success("操作成功,"+errorList);
    }

}

